<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_parent extends MY_Model 
{
	public function get_user_students($userid)
	{
		$q = 'SELECT * FROM parent_child 
				WHERE user_id = ?';
		$res = $this->db->query($q,[$userid]);
		return $res->result();
	}

	public function students($userid)
	{
		$students = $this->get_user_students($userid);

		$student = array();

		foreach($students as $k => $v)
		{
			$student[$k] = $this->get_student_short($v->p_id);
		}

		return $student;

	}

	function get_student_short($profile_id)
	{
		$sq= 'SELECT enrollments.e_id as enrollment_id,
						 enrollments.e_student_id as idno,
						 DATE_FORMAT(enrollments.e_created_at,"%M %e, %Y") as date_registered,
						 DATE_FORMAT(profiles.dob,"%M %e, %Y") as dob_formated,
						 concat_ws("",profiles.lastname," , ",profiles.firstname," ",profiles.middlename) as fullname,
						 enrollments.e_grade_level as level_id,
						 levels.level_desc,
						 enrollments.e_profile_id as pid
				  FROM enrollments
				  LEFT JOIN profiles ON profiles.profile_id = enrollments.e_profile_id
				  LEFT JOIN levels ON levels.level_id = enrollments.e_grade_level
				  WHERE enrollments.e_profile_id = ?
				  ORDER BY enrollments.e_id DESC
				  LIMIT 1';

		$q = $this->db->query($sq,array($profile_id));
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}

	function get_student($profile_id)
	{
		$sq= 'SELECT enrollments.e_id as enrollment_id,
					 concat(employees.first_name," ",employees.last_name) as teacher_name,
					 concat(profiles.sibling_fname1," ",profiles.sibling_lname1) as sibling1,
					 concat(profiles.sibling_fname2," ",profiles.sibling_lname2) as sibling2,
					 concat(profiles.sibling_fname3," ",profiles.sibling_lname3) as sibling3,
					 concat(profiles.sibling_fname4," ",profiles.sibling_lname4) as sibling4,
					 concat(profiles.sibling_fname5," ",profiles.sibling_lname5) as sibling5,
					 concat(profiles.sibling_fname6," ",profiles.sibling_lname6) as sibling6,
					 enrollments.e_student_id as idno,
					 DATE_FORMAT(enrollments.e_created_at,"%M %e, %Y") as date_registered,
					 DATE_FORMAT(profiles.dob,"%M %e, %Y") as dob_formated,
					 concat(profiles.firstname," ",profiles.middlename," ",profiles.lastname) as fullname,
					 enrollments.e_grade_level as level_id,
					 enrollments.e_enrollee_type as student_type,
					 enrollments.e_section_id,
					 levels.level_desc,
					 levels.level_category,
					 sections.section_name,
					 bs.block_name,
					 enrollments.block,
					 enrollments.edit_profile,
					 enrollments.adviser_show_grades,
					 enrollments.finance_show_grades,
					 enrollments.can_login
			  FROM enrollments
			  LEFT JOIN profiles ON profiles.profile_id = enrollments.e_profile_id
			  LEFT JOIN levels ON levels.level_id = enrollments.e_grade_level
			  LEFT JOIN sections ON sections.id = enrollments.e_section_id
			  LEFT JOIN employees ON sections.adviser_id = employees.emp_id
			  LEFT JOIN block_section bs ON enrollments.block = bs.id
			  WHERE enrollments.e_id = ?
			  LIMIT 1; 
			  ';
		$q = $this->db->query($sq,array($profile_id));
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}

	public function get_my_students_commendations($user_id = '')
	{
		$students = $this->students($user_id);

		if($students)
		{
			foreach ($students as $k => $v)
			{
				$_stud[$k]['fullname'] = $v->fullname;
				$_stud[$k]['read'] = $this->_get_my_students_commendations($v->enrollment_id,TRUE);
				$_stud[$k]['unread'] = $this->_get_my_students_commendations($v->enrollment_id);

			}

			return $_stud;
		}else{
			return FALSE;
		}
	}

	private function _get_my_students_commendations($id = '',$read = FALSE)
	{
		$r = $read == FALSE ? 0 : 1;
		$sql = "SELECT sc.id,DATE_FORMAT(sc.created,'%M %d, %Y') as sent,student_id as s_id,
					   CONCAT_WS('',emp.first_name,' ',emp.last_name) as teacher
				FROM student_commendations sc
				LEFT JOIN employees emp ON emp.emp_id = sc.employee
				WHERE sc.student_id = ?
				AND sc.read = ?
				AND sc.ay_id = ?";

		$res = $this->db->query($sql,[$id,$r,$this->c_sy->id]);

		$result['message'] = $res->num_rows() >=1 ? $res->result() : FALSE;
		$result['count'] = $res->num_rows();
		
		return (object)$result;
	}

	public function show_message($id)
	{
		$sql = "SELECT sc.*,
					   CONCAT_WS('',emp.first_name,' ',emp.last_name) as teacher,
					   CONCAT_WS('',p.firstname,' ',p.lastname) as student
				FROM student_commendations sc
				LEFT JOIN employees emp ON emp.emp_id = sc.employee
				LEFT JOIN enrollments e ON e.e_id = sc.student_id
				LEFT JOIN profiles p ON p.profile_id = e.e_profile_id
				WHERE sc.id =?";

		$res = $this->db->query($sql,[$id]);
		return $res->num_rows() >=1 ? $res->row() : FALSE;
	}


}

/* End of file m_parent.php */
/* Location: ./application/models/m_parent.php */